package com.woniuxy.carlife.controller;


import com.alibaba.fastjson.JSON;
import com.woniuxy.carlife.Param.TagsParam;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.Result;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import com.woniuxy.carlife.dto.StoreinfoDto;
import com.woniuxy.carlife.service.StoreInfoService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: StoreInfoController
 *@Description: 门店详情前端控制器
 * @author: yanzhongliang     
 * @date: 2021-04-05
 */

@RestController
@Slf4j
@Api(tags = "门店详情接口信息")//用于描述接口类的相关信息，作用于类上
@RequestMapping("/store-info")
public class StoreInfoController {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private StoreInfoService storeInfoService;

    /**
     * 功能描述〈根据门店ID修改门店营业时间〉
     * @author: yanzhongliang
     * @param: goTime,stopTime
     * @return: null
     * @Date 2021/4/12 11:51
     * @throws
     */

    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "修改门店营业时间,营业状态，门店手机号，服务标签",notes = "<span style='color:red;'>用来修改门店营业时间,营业状态，门店手机号，服务标签的接口</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 70021,message = "门店id为空"),
            @ApiResponse(code=70035,message = "输入信息有误，请重新输入"),
            @ApiResponse(code=70036,message = "修改门店手机号成功"),
            @ApiResponse(code=70037,message = "修改门店营业时间成功"),
            @ApiResponse(code=70038,message = "修改门店营业状态成功"),
            @ApiResponse(code=70039,message = "修改门店服务标签成功"),
            @ApiResponse(code=70040,message = "传入门店营业状态格式不正确")
    })

    @PutMapping("/business")
    public Result updateStoreInfoBusiness(@ModelAttribute  @ApiParam TagsParam tagsParam) {
        //判断门店id是否为空
        if (tagsParam.getStoreId() != null) {
            log.info("修改营业时间开始：参数：{}", tagsParam);
            //如果前端传入门店营业时间不为空，执行修改
            if (tagsParam.getGoTime() != null && tagsParam.getStopTime() != null) {

                //将前端传入开始和结束营业时间进行拼接
                String storeInfoBusiness = ("AM" + " " + tagsParam.getGoTime() + "-" + tagsParam.getStopTime() + " " + "PM");

                //调用数据层方法进行修改
                storeInfoService.updateStoreInfoBusiness(storeInfoBusiness, tagsParam.getStoreId());

                log.info("updateStoreInfoBusiness将门店营业开始和结束时间拼接后和门店ID传入执行修改：参数：{}{}", storeInfoBusiness, tagsParam.getStoreId());

                return new Result(true, StatusCode.UPDATE_STORE_INFO_BUSINESS_SUCCESS, "修改门店营业时间成功");
            } else if (tagsParam.getStoreStatus() != null) {
                if (tagsParam.getStoreStatus().equals("营业中") || tagsParam.getStoreStatus().equals("休息中")) {
                    //如果前端传入门店营业状态不为空时，执行修改门店营业状态
                    storeInfoService.updateStatus(tagsParam.getStoreId(), tagsParam.getStoreStatus());

                    log.info("updateStatus将门店营业状态和门店ID传入执行修改：参数：{}{}", tagsParam.getStoreStatus(), tagsParam.getStoreId());

                    return new Result(true, StatusCode.UPDATE_STORE_STATUS_SUCCESS, "修改门店营业状态成功");
                } else {
                    return new Result(false, StatusCode.STORE_BUSSINESSGS_ERROR, "门店营业状态格式不正确");
                }

            } else if (tagsParam.getTagsNames() != null) {

                //如果前端传入门店服务标签不为空时，执行修改门店服务标签状态
                String tagsName = JSON.toJSONString(tagsParam.getTagsNames());

                storeInfoService.updateTagsName(tagsParam.getStoreId(), tagsName);

                log.info("updateTagsName将门店服务标签和门店ID传入执行修改：参数：{}{}", tagsParam.getTagsNames(), tagsParam.getStoreId());

                return new Result(true, StatusCode.UPDATE_TAGS_SUCCESS, "修改门店服务标签成功");
            } else if (tagsParam.getStorePhone() != null) {
                //如果前端传入门店手机号不为空时，执行修改门店手机号
                storeInfoService.updateStorePhone(tagsParam.getStoreId(), tagsParam.getStorePhone());

                log.info("updateStorePhone将门店手机号和门店ID传入执行修改：参数：{}{}", tagsParam.getStorePhone(), tagsParam.getStoreId());

                return new Result(true, StatusCode.UPDATE_PHONE_SUCCESS, "修改门店手机号成功");
            } else {
                return new Result(false, StatusCode.MESSAGE_ERROR, "传入信息有误，请重新输入");
            }
        }else {
            return new Result(false, StatusCode.ID_EMPTY, "传入门店id为空");
        }
    }

    /**
     * 功能描述〈查询全部门店详情〉
     * @author: yanzhongliang
     * @param: null
     * @return: StoreinfoDto
     * @Date 2021/4/15 11:33
     * @throws
     */

    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "查询全部门店详情",notes = "<span style='color:red;'>用来查询全部门店详情的接口</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 40011,message = "查询为空"),
            @ApiResponse(code=40012,message = "查询成功",response = StoreinfoDto.class),
    })

    @GetMapping("/StoreInfoAndTagsAll")
    public Result getStoreInfoAndTagsAllList(){

        log.info("执行查询所有门店详情方法开始");

        List<StoreinfoDto>list= storeInfoService.getStoreInfoAndTagsListAll();
        //判断查询结果是否为空
        if (!list.isEmpty()){
            log.info("执行查询所有门店详情方法结束：参数：{}",list);
            //将查询到的门店详情转成json字符串
            String strJson= JSON.toJSONString(list);
            // 存入redis
            redisTemplate.opsForHash().put("store","infoList",strJson);
            return new Result(true, StatusCode.SEARCH_SUCCESSFUL, "查询成功",list);
        }else {
            return new Result(false, StatusCode.QUERY_IS_EMPTY, "查询为空");
        }

    }
}

